package com.adorkable.iosdialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 确认取消 提示框
 * lib_business里基于此，再封装成业务相关组件，供app使用
 */
public class AlertDialog {
    private Context context;
    private Dialog dialog;
    private LinearLayout ll_root;
    private TextView tv_title,tv_content,tv_left,tv_right,tv_center,tv_single;
    private View v_line1,v_line2;
    private boolean showTitle = false;
    private boolean showContent = false;
    private boolean showLeftBtn = false;
    private boolean showRightBtn = false;
    private boolean showCenterBtn = false;
    private boolean showSingleBtn = false;

    public static final int ALERT_LEFT = 0;
    public static final int ALERT_RIGHT = 1;
    public static final int ALERT_CENTER = 2;
    public static final int ALERT_SINGLE = 3;
    @IntDef({ALERT_LEFT, ALERT_RIGHT, ALERT_CENTER, ALERT_SINGLE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface AlertOrientation {
    }

    public AlertDialog(Context context) {
        this.context = context;
    }

    public AlertDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.view_alertdialog, null);

        // 获取自定义Dialog布局中的控件
        ll_root = view.findViewById(R.id.ll_root);
        tv_title =  view.findViewById(R.id.tv_title);
        tv_content =  view.findViewById(R.id.tv_content);
        tv_left =  view.findViewById(R.id.tv_left);
        tv_right =  view.findViewById(R.id.tv_right);
        tv_center =  view.findViewById(R.id.tv_center);
        tv_single =  view.findViewById(R.id.tv_single);
        v_line1 =  view.findViewById(R.id.v_line1);
        v_line2 =  view.findViewById(R.id.v_line2);
        tv_title.setVisibility(View.GONE);
        tv_content.setVisibility(View.GONE);
        tv_left.setVisibility(View.GONE);
        tv_right.setVisibility(View.GONE);
        tv_center.setVisibility(View.GONE);
        tv_single.setVisibility(View.GONE);
        v_line1.setVisibility(View.GONE);
        v_line2.setVisibility(View.GONE);

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        // 调整dialog背景大小
        ll_root.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        return this;
    }

    public AlertDialog setTitle(@NonNull String title) {
        if (TextUtils.isEmpty(title)){
            return this;
        }
        showTitle = true;
        tv_title.setText(title);
        return this;
    }

    public AlertDialog setTitle(@StringRes int resId) {
        return setTitle(context.getString(resId));
    }


    public AlertDialog setContent(@NonNull String content) {
        if (TextUtils.isEmpty(content)){
            return this;
        }
        showContent = true;
        tv_content.setText(content);
        return this;
    }

    public AlertDialog setContent(@StringRes int resId) {
        return setContent(context.getString(resId));
    }


    public AlertDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public AlertDialog setLeftButton(String text, float textSizeSP, @ColorInt int textColor, @DrawableRes int testBackgroundShapeResId,
                                     final IDialogClickListener listener) {
        showLeftBtn = true;
        if (TextUtils.isEmpty(text)) {
            tv_left.setText("取消");
        } else {
            tv_left.setText(text);
        }
        if (textSizeSP>0){
            tv_left.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSizeSP);
        }else {
            tv_left.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        }
        if (textColor>0){
            tv_left.setTextColor(textColor);
        }else {
            tv_left.setTextColor(Color.parseColor("#333333"));
        }
        if (testBackgroundShapeResId>0){
            tv_left.setBackgroundResource(testBackgroundShapeResId);
        }else{
            tv_left.setBackgroundResource(R.drawable.alertdialog_shape_corner_left_bottom_white);
        }

        tv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(ALERT_LEFT);
                dialog.dismiss();
            }
        });
        return this;
    }

    public AlertDialog setLeftButton(@StringRes int resId,
                                         final IDialogClickListener listener) {
        return setLeftButton(context.getString(resId),0,0,0,listener);
    }

    public AlertDialog setRightButton(String text,float textSizeSP,int textColor,int testBackgroundShapeResId,
                                         final IDialogClickListener listener) {
        showRightBtn = true;
        if (TextUtils.isEmpty(text)) {
            tv_right.setText("确定");
        } else {
            tv_right.setText(text);
        }
        if (textSizeSP>0){
            tv_right.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSizeSP);
        }else {
            tv_right.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        }
        if (textColor>0){
            tv_right.setTextColor(textColor);
        }else {
            tv_right.setTextColor(Color.parseColor("#FFFFFF"));
        }
        if (testBackgroundShapeResId>0){
            tv_right.setBackgroundResource(testBackgroundShapeResId);
        }else{
            tv_right.setBackgroundResource(R.drawable.alertdialog_shape_cornor_right_bottom_red);
        }

        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(ALERT_RIGHT);
                dialog.dismiss();
            }
        });
        return this;
    }

    public AlertDialog setRightButton(@StringRes int resId,
                                         final IDialogClickListener listener) {
        return setRightButton(context.getString(resId),0,0,0,listener);
    }

    public AlertDialog setCenterButton(String text,float textSizeSP,int textColor,int testBackgroundShapeResId,
                                      final IDialogClickListener listener) {
        showCenterBtn = true;
        if (TextUtils.isEmpty(text)) {
            tv_center.setText("清空");
        } else {
            tv_center.setText(text);
        }
        if (textSizeSP>0){
            tv_center.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSizeSP);
        }else {
            tv_center.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        }
        if (textColor>0){
            tv_center.setTextColor(textColor);
        }else {
            tv_center.setTextColor(Color.parseColor("#333333"));
        }
        if (testBackgroundShapeResId>0){
            tv_center.setBackgroundResource(testBackgroundShapeResId);
        }else{
            tv_center.setBackgroundResource(R.drawable.alertdialog_shape_solid_white);
        }

        tv_center.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(ALERT_CENTER);
                dialog.dismiss();
            }
        });
        return this;
    }

    public AlertDialog setCenterButton(@StringRes int resId,
                                      final IDialogClickListener listener) {
        return setCenterButton(context.getString(resId),0,0,0,listener);
    }

    public AlertDialog setSingleButton(String text,float textSizeSP,int textColor,int testBackgroundShapeResId,
                                       final IDialogClickListener listener) {
        showSingleBtn = true;
        if (TextUtils.isEmpty(text)) {
            tv_single.setText("确认");
        } else {
            tv_single.setText(text);
        }
        if (textSizeSP>0){
            tv_single.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSizeSP);
        }else {
            tv_single.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        }
        if (textColor>0){
            tv_single.setTextColor(textColor);
        }else {
            tv_single.setTextColor(Color.parseColor("#333333"));
        }
        if (testBackgroundShapeResId>0){
            tv_single.setBackgroundResource(testBackgroundShapeResId);
        }else{
            tv_single.setBackgroundResource(R.drawable.alertdialog_shape_solid_white);
        }

        tv_single.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(ALERT_SINGLE);
                dialog.dismiss();
            }
        });
        return this;
    }

    public AlertDialog setSingleButton(@StringRes int resId,
                                       final IDialogClickListener listener) {
        return setSingleButton(context.getString(resId),0,0,0,listener);
    }

    private void setLayout() {
        if (!showTitle && !showContent) {
            tv_title.setText("未设置提示");
            tv_title.setVisibility(View.VISIBLE);
        }
        if (showTitle) {
            tv_title.setVisibility(View.VISIBLE);
        }
        if (showContent) {
            tv_content.setVisibility(View.VISIBLE);
        }
        if (!showLeftBtn && !showRightBtn &&!showCenterBtn &&!showSingleBtn) {
            showSingleBtn=true;
        }
        if (showLeftBtn && showRightBtn &&!showCenterBtn&&!showSingleBtn) {
            tv_left.setVisibility(View.VISIBLE);
            tv_right.setVisibility(View.VISIBLE);
            v_line1.setVisibility(View.VISIBLE);
        }
        if (showLeftBtn && showRightBtn && showCenterBtn&&!showSingleBtn) {
            tv_left.setVisibility(View.VISIBLE);
            tv_right.setVisibility(View.VISIBLE);
            tv_center.setVisibility(View.VISIBLE);
            v_line1.setVisibility(View.VISIBLE);
            v_line2.setVisibility(View.VISIBLE);
        }
        if (showSingleBtn) {
            tv_single.setVisibility(View.VISIBLE);
        }
    }

    public void show() {
        setLayout();
        dialog.show();
    }

    public void dismiss(){
        dialog.dismiss();
    }

    public boolean isShowing(){
        return dialog.isShowing();
    }
}
